home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / post1.0 / ui / example-swing / BoxLayoutDemo.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  3.2 KB  |  116 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.awt.*;
  18. import java.awt.event.*;
  19. import com.sun.java.swing.BoxLayout;
  20. import com.sun.java.swing.JComponent;
  21.  
  22. public class BoxLayoutDemo {
  23.     static int NUM_SQUARES = 5;
  24.     static float[] alignment = {0.0f, 0.25f, 0.5f, 0.75f, 1.0f};
  25.  
  26.     public static void main(String[] args) {
  27.     Panel panel = new Panel();
  28.            panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
  29.  
  30.     /* Create the squares. */
  31.     Square square = null;
  32.     for (int i = 0; i < BoxLayoutDemo.NUM_SQUARES; i++) {
  33.         square = new Square();
  34.         square.setAlignmentX(0.5f);
  35.         square.setAlignmentY(alignment[i]);
  36.         square.normalHue = Color.getHSBColor(alignment[i], 0.4f, 0.85f);
  37.         panel.add(square);
  38.     }
  39.  
  40.     /* Create the instructions. */
  41.     Label label = new Label("Click a box to change its Y alignment.");
  42.  
  43.     WindowListener l = new WindowAdapter() {
  44.         public void windowClosing(WindowEvent e) {System.exit(0);}
  45.     };
  46.  
  47.     Frame f = new Frame("Test Box Layout");
  48.     f.add("Center", panel);
  49.     f.add("South", label);
  50.     f.addWindowListener(l); 
  51.     f.pack();
  52.     f.show();
  53.     }
  54. }
  55.  
  56.  
  57. class Square extends JComponent {
  58.     public Color normalHue;
  59.     private final Dimension preferredSize;
  60.  
  61.     public Square() {
  62.     int pixelsPerSide = (int)(50.0 * Math.random()) + 50;
  63.     preferredSize = new Dimension(pixelsPerSide, pixelsPerSide);
  64.  
  65.     MouseListener l = new MouseAdapter() {
  66.         public void mousePressed(MouseEvent e) {
  67.         Dimension size = getSize();
  68.         float alignment = (float)(e.getY())
  69.                 / (float)size.height;
  70.  
  71.         // Round to the nearest 1/20th.
  72.         int tmp = Math.round(alignment * 20.0f); 
  73.         alignment = (float)tmp / 20.0f;
  74.  
  75.         setAlignmentY(alignment);
  76.         invalidate();
  77.         getParent().validate();
  78.         }
  79.     };
  80.     addMouseListener(l);
  81.     }
  82.  
  83.     public void paint(Graphics g) {
  84.     Dimension size = getSize();
  85.     float alignmentY = getAlignmentY();
  86.  
  87.     g.setColor(normalHue);
  88.     g.fill3DRect(0, 0, size.width, size.height, true);
  89.  
  90.     /* Draw a horizontal white line at the alignment point.*/
  91.     g.setColor(Color.white);
  92.     int y = (int)(alignmentY * (float)size.height) - 1;
  93.     g.drawLine(0, y, size.width - 1, y);
  94.  
  95.     /* Say what the alignment point is. */
  96.     g.setColor(Color.black);
  97.     g.drawString(Float.toString(alignmentY), 3, size.height - 3);
  98.  
  99.     super.paint(g);
  100.     }
  101.  
  102.  
  103.     public Dimension getPreferredSize() {
  104.     return preferredSize;
  105.     }
  106.  
  107.     public Dimension getMaximumSize() {
  108.     return preferredSize;
  109.     }
  110.  
  111.     public Dimension getMinimumSize() {
  112.     return preferredSize;
  113.     }
  114. }
  115.  
  116.